<title>学生申请</title>

<div class="layui-card layadmin-header">
    <div class="layui-breadcrumb" lay-filter="breadcrumb">
        <a><cite>学生申请</cite></a>
    </div>
</div>

<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-header">学生申请</div>
                <div class="layui-card-body">
                    <table class="layui-hide" id="course-table-page" lay-filter="course-page"></table>
                </div>
            </div>
        </div>
    </div>
</div>

<script type="text/html" id="lineBar">
    <a class="layui-btn layui-btn-xs" lay-event="reject_application">拒绝</a>
    <a class="layui-btn layui-btn-xs" lay-event="pass_application">通过</a>
</script>


<script>
    layui.use(['jquery', 'table'], function(){
        var $ = layui.$, form = layui.form,
            table = layui.table, admin = layui.admin,
            view = layui.view;
        var width = window.screen.availWidth > 600 ? 600 : window.screen.availWidth;
        var tablelns = table.render({
            elem: '#course-table-page'
            ,url: "/teacher/course/application"
            ,method: 'get'
            ,cols: [[
                {field:'username',   title: '学生', align: 'center'}
                ,{field:'course_name', title:'课程', align:'center'}
                ,{align:'center',  title:'操作', toolbar:'#lineBar'}
            ]]
            ,parseData: function(res) {
                return {
                    "status": res.status,
                    "message": res.message,
                    "list": res.data
                };
            }
            ,response: {
                statusName: 'status'
                ,statusCode: 200
                ,msgName: 'message'
                ,countName: 'total'
                ,dataName: 'list'
            }
        });


        table.on('tool(course-page)', function (obj) {
            let status = 0;
            switch (obj.event) {
                case 'reject_application':
                    status = 2;
                    break;
                case 'pass_application':
                    status = 1;
                    break;
            }

            let postData = {
                user_id: obj.data.user_id,
                course_id: obj.data.course_id,
                status: status
            }
            let load = layer.load(2);
            $.ajax({
                url:        '/teacher/course/application',
                data:       postData,
                type:       'POST',
                complete    :function (result) {
                    layer.close(load);
                    result = result.responseJSON;
                    if (result.status  === 200) {
                        layui.index.render();
                    }
                    showMsg(result.message);
                }
            })
        });

    });

    function time(time = +new Date()) {
        var date = new Date(time + 8 * 3600 * 1000); // 增加8小时
        return date.toJSON().substr(0, 19).replace('T', ' ');
    }

    //显示结果
    function showMsg(text) {
        layer.msg("<div style=\"padding: 20px 100px;\">"+ text + "</div>", {
            time: 8000, //8s后自动关闭
            btn: ['明白了'],
            shade: 0.3,
            yes: function (index, layero) {
                layer.close(index);
            }
        });
    }
</script>